package com.kxf.androidtestdemo.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewAnimationUtils;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.kxf.androidtestdemo.R;

/**
 * 视图动画（View 动画）
 * 帧动画（Frame 动画、Drawable 动画）
 * 属性动画
 * 触摸反馈动画（Ripple Effect）
 * 揭露动画（Reveal Effect）
 * 转场动画 & 共享元素（Activity 切换动画）
 * 视图状态动画（Animate View State Changes）
 * 矢量图动画（Vector 动画）
 * 约束布局实现的关键帧动画（ConstraintSet 动画）
 *
 * 作者：kxf
 */
public class AnimationActivity extends AppCompatActivity {

    private ImageView iv_frameani, iv_frameani_demo;
    private TextView tv_obj_ani, tv_circular_reveal;
    private Button btn_circular_reveal;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_animation);

        initView();
    }

    private void initView() {
        iv_frameani = findViewById(R.id.iv_frameani);
        iv_frameani_demo = findViewById(R.id.iv_frameani_demo);
        tv_obj_ani = findViewById(R.id.tv_obj_ani);
        tv_circular_reveal = findViewById(R.id.tv_circular_reveal);
        btn_circular_reveal = findViewById(R.id.btn_circular_reveal);


        //获取背景，并将其强转成AnimationDrawable
        AnimationDrawable animationDrawable = (AnimationDrawable) iv_frameani.getBackground();
        animationDrawable.start();

        //创建一个AnimationDrawable
        AnimationDrawable animationDrawable1 = new AnimationDrawable();
        //准备好资源图片
        int[] ids = {R.drawable.loading1,R.drawable.loading2,R.drawable.loading3,R.drawable.loading4};
        //通过for循环添加每一帧动画
        for(int i = 0 ; i < 4 ; i ++){
            Drawable frame = getResources().getDrawable(ids[i]);
            //设定时长
            animationDrawable1.addFrame(frame,80);
        }
        animationDrawable1.setOneShot(false);
        //将动画设置到背景上
        iv_frameani_demo.setBackground(animationDrawable1);
        //开启帧动画
        animationDrawable1.start();

        tv_circular_reveal.post(new Runnable() {
            @SuppressLint("NewApi")
            @Override
            public void run() {

                btn_circular_reveal.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int width = tv_circular_reveal.getWidth();
                        int height = tv_circular_reveal.getHeight();
                        int viewHypotenuse =
                                (int) Math.hypot(width, height); //2
                        final Animator anim = ViewAnimationUtils.createCircularReveal(
                                tv_circular_reveal, 0, 0, 0f, viewHypotenuse);
                        anim.setDuration(1000);
//                        if (anim.isRunning()){
//                            anim.end();
//                        }
                        try {
                            anim.start();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        });

    }

    public void onClick(View view) {
        Log.i("AnimationActivity", "view=" + view);
        Animation animation = AnimationUtils.loadAnimation(AnimationActivity.this,R.anim.view_anim);
        view.startAnimation(animation);
    }

    public void rotationAnimi(View view) {
        //将TextView进行一次360度的旋转
        ObjectAnimator animator = ObjectAnimator.ofFloat(tv_obj_ani, "rotation", 0f, 360f);
        animator.setDuration(2500);
        animator.setRepeatCount(3);
        animator.start();
    }

    public void translationXAnimi(View view) {
        float distance = tv_obj_ani.getTranslationX();
//将TextView先向左移出屏幕，然后再移动回来
        ObjectAnimator translation = ObjectAnimator.ofFloat(tv_obj_ani, "translationX", distance, -500f, distance);
        translation.setDuration(2500);
        translation.start();
    }

    public void scaleYAnimi(View view) {
        //TextView在垂直方向上放大3倍再还原
        ObjectAnimator scale = ObjectAnimator.ofFloat(tv_obj_ani, "scaleY", 1f, 3f, 1f);
        scale.setDuration(2500);
        scale.start();
    }

    public void alphaYAnimi(View view) {
        //TextView透明变化
        ObjectAnimator alpha = ObjectAnimator.ofFloat(tv_obj_ani, "alpha", 1f, 0f, 1f);
        alpha.setDuration(2500);
        alpha.start();
    }
}
